package net.uhuijia.user.userverifycode.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.config.SysSettingConfigHelper;
import net.uhuijia.constants.DBConst;
import net.uhuijia.sys.syssetting.clientservice.SysSettingClientService;
import net.uhuijia.user.userverifycode.persistence.dao.UserVerifycodeDao;
import net.uhuijia.user.userverifycode.persistence.model.UserVerifycode;
import net.uhuijia.user.userverifycode.persistence.model.UserVerifycodeExample;
import net.uhuijia.user.userverifycode.persistence.model.UserVerifycodeExample.Criteria;
import net.uhuijia.user.userverifycode.service.UserVerifycodeService;
import net.uhuijia.utils.CalendarHelper;
import net.uhuijia.utils.RandomStringHelper;
import net.uhuijia.utils.StringHelper;


@Service
@Transactional
public class UserVerifycodeServiceImpl
     extends CrudService<UserVerifycodeDao,UserVerifycode,UserVerifycodeExample>
     implements  UserVerifycodeService {

	@Autowired
	private SysSettingClientService sysSettingClientService;
	
	@Override
	public boolean isEffectiveCode(String phone, String code) {
		boolean result = false;
		if( StringHelper.isNotBlanks(phone,code) ){
			UserVerifycodeExample example = new UserVerifycodeExample();
			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andCodeEqualTo(code);
			criteria.andExpiredTimeGreaterThanOrEqualTo(new Date());
			criteria.andStatusEqualTo(DBConst.FALSE);//0为未验证，还可以使用，1为已验证了 不能再用了
			List<UserVerifycode> list = this.list(example);
			if( !list.isEmpty() ){
				result = true;
			}
		}
		return result;
	}

	@Override
	public String sendCodeByCx(String phone) {
		String randomNumStr = "123456";
		//是否真实发送验证码
		String isSendMsg = SysSettingConfigHelper.isSendMsg.getValue();
		String valueByKey = sysSettingClientService.getValueByKey(SysSettingConfigHelper.isSendMsg.getKey());
	    if( StringHelper.isNotBlanks(valueByKey) ){
	    	isSendMsg = valueByKey;
	    }
	    if( DBConst.TRUE.equals(isSendMsg) ){
	    	randomNumStr = RandomStringHelper.randomNumStr(6);
	    	//创蓝
			//阿里大鱼
			//腾讯
			//维信
			//华信
			//辰希
	    }
		UserVerifycode verifycode = new UserVerifycode();
		this.initDefalutFields(verifycode);
		verifycode.setCode(randomNumStr);
		verifycode.setExpiredTime(getVerifyCodeExpiredTime());
		verifycode.setPhone(phone);
		verifycode.setStatus(DBConst.FALSE);
		this.add(verifycode);
		return SUCCESS;
	}
	
	/**
	 * 获得验证码的失效时间
	 * @return
	 */
	public Date getVerifyCodeExpiredTime(){
		Date date = new Date();
	    String tokenExpiredDay = SysSettingConfigHelper.verifycodeExpiredMinute.getValue();
	    String valueByKey = sysSettingClientService.getValueByKey(SysSettingConfigHelper.verifycodeExpiredMinute.getKey());
	    if( StringHelper.isNotBlanks(valueByKey) ){
	    	tokenExpiredDay = valueByKey;
	    }
	    Date dateAfter = CalendarHelper.getMinuteAfter(date, Integer.parseInt(tokenExpiredDay));
	    return dateAfter;
	}
	
	
}